package com.vancouver.knows.ui.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.ModifyPasswordTaskMark;

public class ModifyPasswordActivity extends BaseActivity implements IResultReceiver {

	private Context mContext;
	@Bind(R.id.common_tips)
	TextView  mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;
	@Bind(R.id.code)
	MyAutoCompleteTextView mCodeEdt;
	@Bind(R.id.password)
	MyAutoCompleteTextView mPasswordEdt;
	@Bind(R.id.reset)
	Button mResetBtn;
	private LoadingDialog loadingDialog;
	private String mAccount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_modify_psd);
		mContext = ModifyPasswordActivity.this;
		setViews();
		setListeners();
	}

	private void setViews() {
		loadingDialog = new LoadingDialog(mContext);
		Intent intent = getIntent();
		String tips = intent.getStringExtra("COMMON_TIPS");
		mAccount = intent.getStringExtra("ACCOUNT");
		mCommonTips.setText(tips);
	}
	
	private void setListeners(){
		mCommonBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@OnClick(R.id.reset)
	public void onResetClick() {
		if (mCodeEdt.getText().toString().trim().equals("") || mPasswordEdt.getText().toString().trim().equals("")){
			Toast.makeText(mContext, mContext.getText(R.string.login_Tips), Toast.LENGTH_SHORT).show();
			return;
		}
		
		BusinessModule bModule = imContext.getBusinessModule();
		ModifyPasswordTaskMark taskMark = bModule.getTaskMarkPool().getModifyPasswordTaskMarkList();

		//判断输入的是手机号验证还是邮箱验证
		if (mAccount.contains("@")) {
			bModule.getServiceWrapper().modifyPassword(ModifyPasswordActivity.this, taskMark, "", mAccount
					, mCodeEdt.getText().toString().trim(), mPasswordEdt.getText().toString().trim());
		}else{
			bModule.getServiceWrapper().modifyPassword(ModifyPasswordActivity.this, taskMark, mAccount, ""
					, mCodeEdt.getText().toString().trim(), mPasswordEdt.getText().toString().trim());
		}
		if (loadingDialog != null)
			loadingDialog.showLoadingDialog(mContext.getString(R.string.reset_loading));
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadingDialog != null){
			loadingDialog.dismiss();
		}
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (loadingDialog != null)
			loadingDialog.closeLoadingDialog();
		if (exception != null && !exception.getExMessage().equals("")){
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(mContext, mContext.getString(R.string.password_reset_success), Toast.LENGTH_SHORT).show();
			finish();
		}
	}
	

}